package org.example.demo.Controller;

import org.example.demo.ResponseHttps.ResponseData;
import org.example.demo.SQL.Dao.UserDao;
import org.example.demo.SQL.Models.User;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    // 查询单个用户
    @GetMapping("/{id}")
    public ResponseData getUserById(@PathVariable("id") Integer id) {
        User user = UserDao.findUserById(id);
        return new ResponseData("200", "查询成功", true, user);
    }

    // 查询所有用户
    @GetMapping("/all")
    public ResponseData getAllUsers() {
        Map<Integer, User> users = UserDao.findAllUsers();
        return new ResponseData("200", "查询成功", true, users);
    }

    // 添加用户
    @PostMapping("/add")
    public ResponseData addUser(@RequestBody User user) {
        UserDao.addUser(user);
        return new ResponseData("200", "添加成功", true, null);
    }

    // 删除用户
    @DeleteMapping("/delete/{id}")
    public ResponseData deleteUser(@PathVariable("id") Integer id) {
        UserDao.deleteUserById(id);
        return new ResponseData("200", "删除成功", true, null);
    }
}
